package com.jueling.culture.controller;

import com.jueling.culture.core.Result;
import com.jueling.culture.core.ResultGenerator;
import com.jueling.culture.dto.req.AgentVO;
import com.jueling.culture.model.AgentConfig;
import com.jueling.culture.model.AgentInfo;
import com.jueling.culture.service.AgentConfigService;
import com.jueling.culture.service.AgentInfoService;
import com.jueling.culture.support.annotation.RequiredPermission;
import com.jueling.culture.support.enums.AuthorityCodeEnum;
import com.jueling.culture.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
* @author linp
* @date 2022/04/09
*/
@RestController
public class AgentInfoController {

    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private AgentConfigService agentConfigService;

    @PostMapping("/api/agent/info")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.AGENCY_MANAGE, edit = true)
    public Result add(@RequestBody AgentInfo agentInfo) {
        //查询代理级别
        AgentConfig agentConfig = agentConfigService.getAgentConfig(agentInfo.getDljbId());
        agentInfo.setDljb(agentConfig.getDljb());
        agentInfo.setDljbmc(agentConfig.getDljbmc());
        agentInfoService.saveAgentInfo(agentInfo);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/api/agent/info/{id}")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.AGENCY_MANAGE, edit = true)
    public Result delete(@PathVariable Integer id) {
        agentInfoService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping("/api/agent/info")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.AGENCY_MANAGE, edit = true)
    public Result update(@RequestBody AgentInfo agentInfo) {
        //查询代理级别
        AgentConfig agentConfig = agentConfigService.getAgentConfig(agentInfo.getDljbId());
        agentInfo.setDljb(agentConfig.getDljb());
        agentInfo.setDljbmc(agentConfig.getDljbmc());
        agentInfoService.updateAgentInfo(agentInfo);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/api/agent/info/{id}")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.AGENCY_MANAGE, view = true)
    public Result detail(@PathVariable Integer id) {
        AgentInfo agentInfo = agentInfoService.findById(id);
        return ResultGenerator.genSuccessResult(agentInfo);
    }

    @GetMapping("/api/agent/info")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.AGENCY_MANAGE, view = true)
    public Result list(@ModelAttribute AgentVO agentVO) {
        return ResultGenerator.genSuccessResult(agentInfoService.getAgrntList(agentVO));
    }

    /**
     * 验证上级代理人是否存在
     * @param custId
     * @return
     */
    @GetMapping("/api/check/agent/{custId}")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.AGENCY_MANAGE, view = true)
    public Result checkAgent(@PathVariable String custId) {
        return ResultGenerator.genSuccessResult(agentInfoService.checkAgent(custId));
    }

    /**
     * 获取代理人树
     * @return
     */
    @GetMapping("/api/get/agent/user")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.AGENCY_MANAGE, view = true)
    public Result getAgentUserList() {
        return ResultGenerator.genSuccessResult(agentInfoService.getAgentUserList());
    }



    /**
     * 刷数据
     * @return
     */
    @GetMapping("/refush/agent/user/{code}")
    public Result refushAgent(@PathVariable String code) {
        return ResultGenerator.genSuccessResult(agentInfoService.refushAgent(code));
    }

    /**
     * 修改用户状态
     * @param custId
     * @return
     */
    @GetMapping("/api/update/agent/user/{custId}")
    public Result updateAgentState(@PathVariable String custId) {
        agentInfoService.updateAgentState(custId);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 导出
     * @return
     */
    @GetMapping("/api/down/load/data")
    public void downloadData(@ModelAttribute AgentVO agentVO, HttpServletResponse response) {
        List<List<Object>> allAgentData = agentInfoService.getAllAgentData(agentVO);
        List<String> channels = new ArrayList<>();
        channels.add("代理名称");
        channels.add("代理编号");
        channels.add("手机号");
        channels.add("注册日期");
        channels.add("是否虚拟用户");
        channels.add("实际用户");
        channels.add("代理级别");
        channels.add("状态");
        channels.add("推荐人");
        channels.add("新推荐人");
        channels.add("开户行名称");
        channels.add("开户名");
        channels.add("银行卡号");
        channels.add("收获地址");
        ExcelUtil.exportExcel(allAgentData,channels, response ,"代理明细");
    }


}
